﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using IMCOM.Imagiality.Core;
using IMCOM.Imagiality.ImageProcessing.Canvas2D.Commands;
namespace IMCOM.Imagiality.ImageProcessing.Canvas2D
{
    public class Canvas2DButtonEventListener
    {
        private CanvasImageWrapper canvasImageWrapper;
        private Canvas2DGUI canvas2DGUI;

        public Canvas2DButtonEventListener(Canvas2DGUI canvas2DGUI, CanvasImageWrapper canvasImageWrapper)
        {
            this.canvas2DGUI = canvas2DGUI;
            this.canvasImageWrapper = canvasImageWrapper;
        }

        public void ZoomInClicked(object sender, EventArgs e)
        {
            ZoomInCommand zoomInCommand = new ZoomInCommand();
            canvasImageWrapper.ExecuteCommand(zoomInCommand);
        }

        public void ZoomOutClicked(object sender, EventArgs e)
        {
            ZoomOutCommand zoomOutCommand = new ZoomOutCommand();
            canvasImageWrapper.ExecuteCommand(zoomOutCommand);
        }

        public void ZoomOriginalClicked(object sender, EventArgs e)
        {
            double oldScalingFactor = canvasImageWrapper.ScalingFactor;

            ZoomOriginalCommand zoomOriginalCommand = new ZoomOriginalCommand();
            canvasImageWrapper.ExecuteCommand(zoomOriginalCommand);
        }

        public void ZoomFitClicked(object sender, EventArgs e)
        {
            Size size = canvas2DGUI.getContentPanelSize();
            Bitmap activeBitmap = canvasImageWrapper.ActiveImage.getBitmap();

            double verticalRatio = (double)size.Height / (double)activeBitmap.Height;
            double horizontalRatio = (double)size.Width / (double)activeBitmap.Width;

            if (verticalRatio < horizontalRatio)
            {
                canvasImageWrapper.ExecuteCommand(new ZoomFitCommand(verticalRatio));
                //          RefreshAutoContrastCurrentState(verticalRatio);
            }
            else
            {
                canvasImageWrapper.ExecuteCommand(new ZoomFitCommand(horizontalRatio));
                //        RefreshAutoContrastCurrentState(horizontalRatio);
            }
        }
    }
}
